package cn.onecloud.tablet.xiaozhi.net;

import com.freerdp.freerdpcore.utils.Log;
import com.google.gson.Gson;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

import cn.onecloud.tablet.xiaozhi.model.DesktopGroupMessage;
import cn.onecloud.tablet.xiaozhi.model.DesktopInfo;
import cn.onecloud.tablet.xiaozhi.model.SingleDesktopMessage;


/**
 * Created by yin.he on 17-11-14.
 */
public class PullResult {


    /**
     * @param uuidKey
     * @return
     */
    public static boolean connectAppcc(final String uuidKey) {
        String appccServer = "appcc.cloudak47.com";
        boolean isSsl = false;
        String url = null;
        if (!isSsl) {
            url = "ws://" + appccServer + "/connection/wait?key=" + uuidKey;
        } else {
            url = "wss://" + appccServer + "/connection/wait?key=" + uuidKey;
        }
        Log.d(PullResult.class.getName(), "websocket url : " + url);
        try {
            final WebsocketClientEndpoint clientEndPoint = new WebsocketClientEndpoint(new URI(url), isSsl);
            clientEndPoint.addMessageHandler(new WebsocketClientEndpoint.MessageHandler() {

                public void handleMessage(String message) {

                    WSThread.isScan = false;
                    WSThread.isLogin = false;
                    WSThread.isDeskgroup = false;
                    Gson gson = new Gson();
                    Action action = Action.UNKNOWN;
                    WebSocketResult webSocketResult = new WebSocketResult();

                    CommonMessage commonMsg = gson.fromJson(message, CommonMessage.class);
                    action = Action.getActionByMsg(commonMsg.getAction());

                    switch (action) {
                        case SCAN:
                            webSocketResult.setUuid(uuidKey);
                            Log.d(PullResult.class.getName(), " uuidkey scan >>> " + uuidKey);
                            webSocketResult.setWsResult(new WSResult(action, "success"));
                            WSThread.isScan = true;
                            WSThread.webSocketResult = webSocketResult;
                            break;
                        case LOGIN:
                            if (commonMsg.getData_type() == null) {
                                SingleDesktopMessage singleDeskMsg = gson.fromJson(message, SingleDesktopMessage.class);
                                DesktopInfo desktopInfo = singleDeskMsg.getData();
                                WSThread.desktopInfo = desktopInfo;
                                WSThread.isLogin = true;
                                Log.d(PullResult.class.getName(), " uuidkey login >>> " + uuidKey);
                                webSocketResult.setUuid(uuidKey);
                                webSocketResult.setWsResult(new WSResult(action, "success"));
                                WSThread.isScan = true;
                                WSThread.webSocketResult = webSocketResult;
                            } else if ("list".equalsIgnoreCase(commonMsg.getData_type())) {
                                WSThread.isScan = true;
                                WSThread.isLogin = true;
                                WSThread.isDeskgroup = true;
                                DesktopGroupMessage groupDeskMsg = gson.fromJson(message, DesktopGroupMessage.class);
                                if (groupDeskMsg != null) {
                                    List<DesktopInfo> desktopInfoList = groupDeskMsg.getData();
                                    if (desktopInfoList != null && desktopInfoList.size() > 0) {
                                        WSThread.desktopInfoList = desktopInfoList;
                                    }
                                }
                                webSocketResult = new WebSocketResult();
                                webSocketResult.setUuid(uuidKey);
                                webSocketResult.setWsResult(new WSResult(action, "success"));
                            }
                            break;
                        case CANCEL:
                            break;
                        default:
                            break;
                    }
                }
            });
            return true;
        } catch (URISyntaxException ex) {
            ex.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

